<?php

class DB
{
	protected static $_dbh = null;

	static function prepare($query)
	{
		return self::getInstance()->prepare($query);
	}
	
	static function execute($query)
	{
		return self::getInstance()->exec($query);
	}

	protected function getInstance()
	{
		if(!self::$_dbh)
		{
			self::$_dbh = self::create();
		}

		return self::$_dbh;
	}
	
	private function create()
	{
		require("config/config.php");

		$dbh = new PDO($_CONFIG["dsn"], $_CONFIG["dbuser"], $_CONFIG["dbpassword"]);
		$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
		$dbh->setAttribute(PDO::ATTR_CASE, PDO::CASE_LOWER);
		if (preg_match('/^sqlite\:/', $_CONFIG['dsn']))
		{
			$result = $dbh->exec('PRAGMA foreign_keys');
			if ($result === 0 || $result === 1)
			{
				$dbh->exec('PRAGMA foreign_keys = ON;');
			}
			else
			{
				die('Database not supported');
			}
		}
		return $dbh;
	}

}

?>
